package cc.laop.business;

import cc.laop.dao.BaseMapper;
import cc.laop.entity.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class BaseBusinessImpl<P extends BaseMapper<T>, T>
        implements BaseBusiness<T> {

    @Autowired
    protected P mapper;

    @Override
    public T selectOne(Object obj) {
        return mapper.selectOne(obj);
    }

    @Override
    public List<T> select(Object obj) {
        return mapper.select(obj);
    }

    @Override
    public PageInfo<T> selectByPage(Object obj, Page page) {
        if (page == null || page.getPageNum() <= 0 || page.getPageSize() <= 0) {
            page = new Page(1, 10);
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<T> list = mapper.select(obj);
        PageInfo<T> pageinfo = new PageInfo<T>(list);
        return pageinfo;
    }

    @Override
    public int insert(T t) {
        if (t != null) {
            return mapper.insert(t);
        }
        return 0;
    }

    @Override
    public int update(T t) {
        if (t != null) {
            return mapper.update(t);
        }
        return 0;
    }

    @Override
    public int delete(Object obj) {
        if (obj != null) {
            return mapper.delete(obj);
        }
        return 0;
    }

    public int delete(Object[] objArray) {
        int result = 0;
        if (ArrayUtils.isNotEmpty(objArray)) {
            for (Object obj : objArray) {
                result += mapper.delete(obj);
            }
        }
        return result;
    }

}
